#include <iostream>
#include "log4cpp/Category.hh"
#include "log4cpp/OstreamAppender.hh"
#include "log4cpp/FileAppender.hh"
#include "log4cpp/PatternLayout.hh"
#include "log4cpp/Priority.hh"
using namespace std;
using namespace log4cpp;

//设计个性化的日志布局需要使用PatternLayout
void test0(){
    //1.设置日志布局
    PatternLayout * ptn1  = new PatternLayout();
    ptn1->setConversionPattern("%d %c [%p] %m%n");

    PatternLayout * ptn2  = new PatternLayout();
    ptn2->setConversionPattern("%d %c [%p] %m%n");

    //2.创建输出器对象
    OstreamAppender * pos = new OstreamAppender("console",&cout);
    //输出器与布局绑定
    pos->setLayout(ptn1);

    FileAppender * filePos = new FileAppender("file","wd.log");
    filePos->setLayout(ptn2);

    //3.创建日志记录器
    //引用名salesDepart是在代码中使用的，表示Category对象
    //参数中salesDepart是获取日志来源时返回的记录器的名字
    //一般让两者相同，方便理解
    Category & salesDepart = Category::getInstance("salesDepart");

    //4.给Category设置优先级
    salesDepart.setPriority(Priority::ERROR);

    //5.给Category设置输出器
    salesDepart.addAppender(pos);
    salesDepart.addAppender(filePos);

    //6.记录日志
    salesDepart.emerg("this is an emerge msg");
    salesDepart.fatal("this is a fatal msg");
    salesDepart.alert("this is an alert msg");
    salesDepart.crit("this is a crit msg");
    salesDepart.error("this is an error msg");
    salesDepart.warn("this is a warn msg");
    salesDepart.notice("this is a notice msg");
    salesDepart.info("this is a info msg");

    //7.日志系统退出时，回收资源
    Category::shutdown();
}

int main(void){
    test0();
    return 0;
}
